﻿using System;
using Aesha.WoW.Broker.Infrastructure;

namespace Aesha.WoW.Broker.Domain
{
    internal class AuctionHouse
    {
        public void Refresh()
        {
            var battleNetRepository = new BattleNetRepository();
            var brokerRepository = new BrokerRepository();

            var auctionUpdate = battleNetRepository.GetLatestAuctionDataUpdate();

            var listings = battleNetRepository.GetListings(new Uri(auctionUpdate.Url));

            var cacheItems = brokerRepository.GetCachItemIds();

            foreach (var listing in listings)
            {
                if (!cacheItems.Contains(listing.ItemId))
                {
                    if (!brokerRepository.ItemExists(listing.ItemId))
                    {
                        var item = battleNetRepository.GetItem(listing.ItemId);

                        if (item != null)
                        {
                            try
                            {
                                brokerRepository.CacheItem(item);
                                cacheItems.Add(item.ItemId);
                            }
                            catch (Exception ex)
                            {
                                continue;
                            }
                        }
                    }
                }
            }

        }
    }
}
